package com.hanvon.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;

public class StrokeView extends View {

    private final Bitmap mBitmap;
    private final int mWidth;
    private final int mHeight;
    private final int[] mPixels;
	private final int[] mBackgroundPixels;
    private final int[] mRect = new int[4];
    
    private static final int ANTI_LEVEL = 3;
	private static final int PEN_STYLE = 2;
	/** 带压感笔锋效果的彩笔核心增加蜡笔风格 */
    private static final int PEN_COLOR_TYPE = 6; /** 带压感笔锋效果的彩笔核心不支持渐变色 */
    private static final int PEN_COLOR_RATE = 30;
	private static final int PEN_WIDTH = 3;
    
	private final float[] points = new float[2048];
    
	public static final int PEN = 1;   /** 该变量用来区分用手还是用笔，与具体的设备有关，它和onTouchEvent函数中的event.getMetaState()值相等，要根据具体设备进行修改 */

    public StrokeView( Context context, int width, int height ) {
        super( context );
        setBackgroundColor(0x77ffffff);
        mWidth = width;
        mHeight = height;
        mBitmap = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
		mBitmap.eraseColor(0x00ffffff);
        mPixels = new int[width * height];
		mBackgroundPixels = new int[width * height];
//		mBitmap.eraseColor(Color.GRAY);
		mBitmap.getPixels(mBackgroundPixels, 0, width, 0, 0, width, height);
        mBitmap.getPixels( mPixels, 0, width, 0, 0, width, height );
        /* TEST */
        HWColorPen.initializeEx( width, height, mPixels );
        HWColorPen.setPen( ANTI_LEVEL, PEN_STYLE, PEN_COLOR_TYPE, PEN_WIDTH, PEN_COLOR_RATE );
        
        /*int ROUND_RADIUS = 20;
        int y = height >> 1;
        int[] r = new int[4];
        HWColorPen.drawLine(ROUND_RADIUS, y, r, mPixels);
        Log.e("-------", "rect:" + r[0] + "," + r[1] + "," + r[2] + "," + r[3]);
        HWColorPen.drawLine(width - ROUND_RADIUS, y, r, mPixels);
        Log.e("-------", "rect:" + r[0] + "," + r[1] + "," + r[2] + "," + r[3]);
        HWColorPen.drawLine(-1, -1, r, mPixels);
        Log.e("-------", "rect:" + r[0] + "," + r[1] + "," + r[2] + "," + r[3]);
        
        mBitmap.setPixels(mPixels, 0, width, 0, 0, width, height);
        invalidate();*/
    }

    @Override
    protected void onDraw( Canvas canvas ) {
        canvas.drawBitmap( mBitmap, 0, 0, null );
    }

    @Override
    public boolean onTouchEvent( MotionEvent event ) {
        final int x = (int) event.getX( );
        final int y = (int) event.getY( );
        
        float pressure = 0.8f;		//手的压感
        
		if(event.getMetaState() == PEN)		//event.getMetaState()的值需要和常量PEN相等，这个根据具体设备要改变PEN的值
			  pressure = event.getPressure();		
        
        if (x < 0 || y < 0 || x > mWidth || y > mHeight) {
            return true;
        }
        switch ( event.getAction( ) ) {
        case MotionEvent.ACTION_DOWN: 
        case MotionEvent.ACTION_MOVE:
        	HWColorPen.drawLineEx( x, y,pressure, mRect, points );
            break;
        default:
        	HWColorPen.drawLineEx( -100, -100, -100f, mRect, points );
            break;
        }
        mBitmap.setPixels( mPixels, mRect[1] * mWidth + mRect[0], mWidth, mRect[0], mRect[1], mRect[2]
                - mRect[0], mRect[3] - mRect[1] );
        invalidate( mRect[0], mRect[1], mRect[2], mRect[3] );
        return true;
    }
    
	public void saveBitmap(String path) throws IOException {
		File f = new File(path);
		f.createNewFile();
		FileOutputStream fOut = null;
		try {
			fOut = new FileOutputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
		try {
			fOut.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			fOut.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
